package com.turboconnard.net;

import java.util.Vector;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class GMLHandler extends DefaultHandler {
	
	private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet();
	private GMLObject gml;

	private boolean _clientName;
	private boolean _clientVersion;
	private boolean _clientUsername;
	private boolean _clientKeywords;
	private boolean _clientUniqueKey;
	private float[] _currentPoint;
	private boolean _time;
	private boolean _z;
	private boolean _y;
	private boolean _x;
	private Vector<float[]> vPoints;
	private boolean _stroke;
	private boolean _screenBounds;

	
	public ParsedExampleDataSet getParsedData() {
		return this.myParsedExampleDataSet;
	}
	@Override
	public void startDocument() throws SAXException {
		this.myParsedExampleDataSet = new ParsedExampleDataSet();
		gml = new GMLObject();
		vPoints = new Vector<float[]>();
	}

	public GMLObject getGML(){
		return gml;
		
	}
	@Override
	public void endDocument() throws SAXException {
		// Nothing to do
		
	}

	/**
	 * Gets be called on opening tags like: <tag> Can provide attribute(s), when
	 * xml was like: <tag attribute="attributeValue">
	 */
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		if (localName.equals("GML")) {
			gml.spec = atts.getValue("spec");
		} else if (localName.equals("name")) {
			_clientName = true;
		} else if (localName.equals("version")) {
			_clientVersion = true;
		} else if (localName.equals("username")) {
			_clientUsername = true;
		} else if (localName.equals("keywords")) {
			_clientKeywords = true;
		} else if (localName.equals("name")) {
			_clientUniqueKey = true;
		} else if (localName.equals("stroke")) {
			_stroke = true;
		} else if (localName.equals("pt")) {
			_currentPoint = new float[4];
		} else if (localName.equals("x")) {
			_x = true;
		} else if (localName.equals("y")) {
			_y = true;
		} else if (localName.equals("z")) {
			_z = true;
		} else if (localName.equals("time")) {
			_time = true;
		} else if (localName.equals("screenBounds")) {
			_screenBounds = true;	
		} else if (localName.equals("zob")) {
			// Extract an Attribute
			String attrValue = atts.getValue("thenumber");
			int i = Integer.parseInt(attrValue);
			myParsedExampleDataSet.setExtractedInt(i);
		}
	}

	/**
	 * Gets be called on closing tags like: </tag>
	 */
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if (localName.equals("name")) {
			_clientName = false;
		} else if (localName.equals("version")) {
			_clientVersion = false;
		} else if (localName.equals("username")) {
			_clientUsername = false;
		} else if (localName.equals("keywords")) {
			_clientKeywords = false;
		} else if (localName.equals("name")) {
			_clientUniqueKey = false;
		} else if (localName.equals("stroke")) {
			_stroke = false;
			Log.d("--------------------------", ("combien des points : " + vPoints.size()) );
			float[][] points = new float[vPoints.size()][4];
			for(int i=0;i< vPoints.size();i++){
				points[i] = vPoints.get(i);				
			}
			gml.points = points;
		} else if (localName.equals("x")) {
			_x = false;
		} else if (localName.equals("y")) {
			_y = false;
		} else if (localName.equals("z")) {
			_z = false;
		} else if (localName.equals("time")) {
			_time = false;
		} else if (localName.equals("screenBounds")) {
			_screenBounds = false;	
		} else if (localName.equals("pt")) {
			vPoints.add(_currentPoint);
		} 
	}

	/**
	 * Gets be called on the following structure: <tag>characters</tag>
	 */
	@Override
	public void characters(char ch[], int start, int length) {
		if (_clientName) gml.clientName = new String(ch, start,length);
		if (_clientVersion) gml.clientVersion = new String(ch, start,length);
		if (_clientUsername) gml.clientUsername = new String(ch, start,length);
		if (_clientKeywords) gml.clientKeywords = new String(ch, start,length);
		if (_clientUniqueKey) gml.clientUniqueKey = new String(ch, start,length);
		
		if (_x && _screenBounds) gml.screenBoundsX = Float.parseFloat(new String(ch, start,length));
		if (_y && _screenBounds) gml.screenBoundsY = Float.parseFloat(new String(ch, start,length));
		if (_z && _screenBounds) gml.screenBoundsZ = Float.parseFloat(new String(ch, start,length));
		
		
		
		if (_x && _stroke) _currentPoint[0] = Float.parseFloat(new String(ch, start,length));
		if (_y && _stroke) _currentPoint[1] = Float.parseFloat(new String(ch, start,length));
		if (_z && _stroke) _currentPoint[2] = Float.parseFloat(new String(ch, start,length));
		if (_time && _stroke) _currentPoint[3] = Float.parseFloat(new String(ch, start,length));
		
	}
}